<?php

include_once '../inc/db_util.php';

function countMahasiswa($nim) {
    if (is_null($nim))
        return mysql_num_rows(mysql_query("SELECT id_mahasiswa FROM tb_mahasiswa"));
    else
        return mysql_num_rows(mysql_query("SELECT id_mahasiswa FROM tb_mahasiswa WHERE nim='" . $nim . "'"));
}

function viewNotApprovedMahasiswa() {
    $array_mhs = array();
    $query = mysql_query("SELECT * FROM tb_temp_mahasiswa m LEFT JOIN tb_angkatan a ON a.id_angkatan = m.id_angkatan");

    while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
        $row_array['Nim'] = $row['nim'];
        $row_array['Nama'] = $row['nama'];
        $row_array['Alamat'] = $row['alamat'];
        $row_array['Telepon'] = $row['telepon'];
        $row_array['Email'] = $row['email'];
        $row_array['TahunAngkatan']['Id'] = $row['id_angkatan'];
        $row_array['TahunAngkatan']['Tahun'] = $row['tahun_angkatan'];

        array_push($array_mhs, $row_array);
    }
    return json_encode($array_mhs);
}

function listOfMahasiswa() {
    $array_mhs = array();
    $query = mysql_query("SELECT * FROM tb_mahasiswa m LEFT JOIN tb_angkatan a ON a.id_angkatan = m.id_angkatan");

    while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
        $row_array['Nim'] = $row['nim'];
        $row_array['Nama'] = $row['nama'];
        $row_array['Alamat'] = $row['alamat'];
        $row_array['Telepon'] = $row['telepon'];
        $row_array['Email'] = $row['email'];
        $row_array['TahunAngkatan']['Id'] = $row['id_angkatan'];
        $row_array['TahunAngkatan']['Tahun'] = $row['tahun_angkatan'];

        array_push($array_mhs, $row_array);
    }
    return json_encode($array_mhs);
}

function viewMahasiswaAccount($json) {
    $array_mhs = array();

    $jsArray = json_decode($json, false);
    $query = mysql_query("SELECT * FROM tb_mahasiswa m LEFT JOIN tb_angkatan t ON m.id_angkatan=t.id_angkatan 
        WHERE nim='" . $jsArray->{'Nim'} . "'");

    while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
        $row_array['Id'] = $row['id_mahasiswa'];
        $row_array['Nim'] = $row['nim'];
        $row_array['Nama'] = $row['nama'];
        $row_array['Username'] = $row['username'];
        $row_array['Alamat'] = $row['alamat'];
        $row_array['Telepon'] = $row['telepon'];
        $row_array['Email'] = $row['email'];
        $row_array['TahunAngkatan']['Id'] = $row['id_angkatan'];
        $row_array['TahunAngkatan']['Tahun'] = $row['tahun_angkatan'];

        array_push($array_mhs, $row_array);
    }
    return json_encode($array_mhs);
}

function updateMahasiswaAccount($json) {
    $jsArray = json_decode($json, false);
    $query = "UPDATE tb_mahasiswa SET 
        nim='" . $jsArray->{'Nim'} . "',
        username='" . $jsArray->{'Username'} . "',
        password='" . md5($jsArray->{'Password'}) . "',
        nama='" . $jsArray->{'Nama'} . "',
        alamat='" . $jsArray->{'Alamat'} . "',
        telepon='" . $jsArray->{'Telepon'} . "',
        email='" . $jsArray->{'Email'} . "',
        id_angkatan='" . $jsArray->{'TahunAngkatan'}->{'Id'} . "'
        WHERE nim='" . $jsArray->{'Id'} . "'";

    return mysql_query($query);
}

function loginMahasiswa($nim, $username, $password) {
    $array_mhs = array();
	$qLoginNoName = "SELECT id_mahasiswa, nama, username FROM tb_mahasiswa WHERE nim='" . $nim . "' AND password='" . md5($password) . "'";
	$qLoginWithName = "SELECT id_mahasiswa, nama, username FROM tb_mahasiswa WHERE nim='" . $nim . "' AND username='" . $username . "' AND password='" . md5($password) . "'";
    $queryLogin = $username == NULL ? $qLoginNoName : $qLoginWithName;
	
	$query = mysql_query($queryLogin);

    while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
        $row_array['LoginUser']['Id'] = $row['id_mahasiswa'];
        $row_array['LoginUser']['IdUser'] = $row['nim'];
        $row_array['LoginUser']['Nama'] = $row['nama'];
        $row_array['LoginUser']['Username'] = $row['username'];
        $row_array['LoginUser']['Type'] = 'Mahasiswa';
        $row_array['Status'] = 'Success';

        array_push($array_mhs, $row_array);
    }

    $result = mysql_num_rows($query);
    
    if ($result > 0) {
        return json_encode($array_mhs);
    } else {
        $row_array['Status'] = 'Login Failed!';
        $row_array['LoginUser'] = NULL;
        
        array_push($array_mhs, $row_array);
        return json_encode($array_mhs);
    }
}

function registerMahasiswa($jsArray, $id_mhs, $id_angkatan) {
    $sql = "INSERT INTO tb_temp_mahasiswa (id_mahasiswa, nim, nama, username, password, alamat, telepon, email, id_angkatan)
        VALUES ('" . $id_mhs . "',
        '" . $jsArray->{'Nim'} . "',
        '" . $jsArray->{'Nama'} . "',
        '" . $jsArray->{'Username'} . "',
        '" . md5($jsArray->{'Password'}) . "',
        '" . $jsArray->{'Alamat'} . "',
        '" . $jsArray->{'Telepon'} . "',
        '" . $jsArray->{'Email'} . "',
        '" . $id_angkatan . "')";

    return mysql_query($sql);
}

?>
